package com.wontube.smr.bo.sites.fs;

import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 
 * @author LuoBiao
 *
 */
public class ObtainFSMp3CountByName extends DefaultHandler {

	private int count = 0;
	private String preTag = null;//作用是记录解析时的上一个节点名称 
	
	public int getMp3Count(InputStream xmlStream) throws ParserConfigurationException, SAXException, IOException{
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser parser = factory.newSAXParser();
		parser.parse(xmlStream, this);
		return count;
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if("total-files".equals(qName)){
			this.count = 0;
		}
		preTag = qName;//将正在解析的节点名称赋给preTag 
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		preTag = null;
	}
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if(preTag != null){  
            String content = new String(ch,start,length);  
            if("total-files".equals(preTag)){  
               this.count = Integer.parseInt(content);  
            }
		}
	}
}
